package server.controller.news;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import server.db.primary.mapper.map.NewsAppMapper;
import server.db.primary.model.map.NewsApp;
import server.tool.Res;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
public class NewsAppController {
    @Autowired
    NewsAppMapper newsAppMapper;


    /**
     * 插入新闻附件
     * @param record
     * @return
     */

    @RequestMapping("/NewsApp/NewsAppInsert")
    public Res NewsAppInsert(NewsApp record) {
        int NewsApp=newsAppMapper.insertSelective(record);
        return Res.success(NewsApp);
    }

    /**
     * 修改新闻附件
     * @param record
     * @return
     */
    @RequestMapping("/NewsApp/NewsAppUpdate")
    public Res NewsAppUpdate(NewsApp record){
        int NewsApp=newsAppMapper.updateByPrimaryKeySelective(record);
        return  Res.success(NewsApp);
    }

    /**
     * 查询全部附件
     * @return
     */
    @RequestMapping("/NewsApp/All")
    public Res NewsAppByAll() {
        List<NewsApp> newsApps = newsAppMapper.selectByAll();
        return Res.success(newsApps);
    }

    /**
     * 实现文件上传.....
     * */
    @RequestMapping("/NewsApp/upload")
    public Res fileupload(NewsApp newsApp, MultipartFile file, HttpServletRequest request) {


        Map<String, Object> result = new HashMap<>();

        String originalFilename = file.getOriginalFilename();
        String substring1 = originalFilename.substring(originalFilename.lastIndexOf("."));
        newsApp.setAppName(originalFilename);

        //获取文件的后缀名
        String substring2 = originalFilename.substring(originalFilename.indexOf("."));

        if (!originalFilename.endsWith(substring2)) {
            return Res.failure(null,"文件格式错误");
        }
        String realPath = request.getServletContext().getRealPath("/");
        String substring = realPath.substring(0, realPath.lastIndexOf("\\"));

        File folder = new File(realPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String newName = UUID.randomUUID() + substring2;

        try {
            file.transferTo(new File(folder, newName));
            String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/" + newName;
            //存localhost
            newsApp.setData(url);
            //路径

            result.put("status", "success");
            result.put("message", "上传成功");
            result.put("url", url);
        } catch (IOException e) {
            result.put("status", "error");
            result.put("messgae", e.getMessage());
        }
        int insert = newsAppMapper.insert(newsApp);
        System.out.println("影响行数---->"+insert);
        return Res.success(result);
    }

    @RequestMapping("/NewsApp/DelectId")
    public Res newsAppDelectId(long id) {
        int i = newsAppMapper.deleteByPrimaryKey(id);
        return Res.success(i);
    }

    /**
     * 根据所有流水号查询所有的数据
     * @param numberId
     * @return
     */
    @RequestMapping("/NewsApp/newsAppBynumberId")
    public Res affcheAppBynumberId(Long numberId) {
        List<NewsApp> news = newsAppMapper.selectByNumberId(numberId);
        return Res.success(news);
    }

}
